from fastmcp import FastMCP
import requests

# Create an MCP server
mcp = FastMCP("Pipeline Analyzer")

# Add an addition tool
@mcp.tool()
def analyze(pipeline_id: str) -> str:
    """Analyze a pipeline run by pipeline_id and return the failure reason if any."""
    # 假设有一个外部 API 用于获取 pipeline 分析结果
    try:
        # 替换为实际的 API 端点
        api_url = f"https://your-pipeline-api.com/analyze"
        response = requests.post(api_url, json={"pipeline_id": pipeline_id})
        if response.status_code == 200:
            analysis_result = response.json()
            return f"Pipeline {pipeline_id} 分析结果: {analysis_result.get('status', '未知')}"
        else:
            return f"Pipeline {pipeline_id} 分析失败，状态码: {response.status_code}"
    except requests.RequestException as e:
        return f"Pipeline {pipeline_id} 分析出错: {str(e)}"

if __name__ == "__main__":
    mcp.run()